class Solution:
    def minFallingPathSum(self, matrix: List[List[int]]) -> int:
        m=len(matrix)
        n=len(matrix[0])
        dp=matrix
        for i in range(1,m):
            for j in range(n):
                if j==0:
                    dp[i][j]+=min(matrix[i-1][j],matrix[i-1][j+1])
                elif j==n-1:
                    dp[i][j]+=min(matrix[i-1][j-1],matrix[i-1][j])
                else:
                    dp[i][j]+=min(matrix[i-1][j-1],matrix[i-1][j],matrix[i-1][j+1])
        return min(dp[-1])